## Dplay have an API but it isn't for public use
## - Requires an country gtld
module Shin
  module Play
    class Dplay

      def new
        self
      end

      # Fix these before running
      def before(params={})
        raise MissingArgument, "You are missing the argument 'dplay_code' which is required to use this source." unless Shin.get[:dplay_code] != nil

        "http://www.dplay." + Shin.get[:dplay_code]
      end

      # Programs
      def programs(params={})
        domain = before()

        # Response
        response = Base.get(domain + '/api/v2/ajax/shows?' + URI.encode_www_form(params))
        raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200

        # Data
        data = Oj.load(response.body) rescue nil

        # Multiple
        if data != nil
          data.to_hashugar
        else
          raise NotValid, "Couldn't parse the JSON"
        end
      end

      # Program
      def program(params={})
        raise MissingArgument, "You are missing the argument 'show_id' which is required to use this source." unless params[:show_id] != ""
        domain = before()

        # Response
        response = Base.get(domain + '/api/v2/ajax/shows/' + params[:show_id].to_s + '?show_id=' + params[:show_id].to_s)
        raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200

        # Data
        data = Oj.load(response.body) rescue nil

        # Can't be nil
        if data != nil
          data.to_hashugar
        else
          raise NotValid, "Couldn't parse the JSON"
        end
      end

      # Videos
      def videos(params={})
        domain = before()

        # Response
        response = Base.get(domain + '/api/v2/ajax/shows/' + params[:show_id] + '/seasons?' + URI.encode_www_form(params))
        raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200

        # Data
        data = Oj.load(response.body) rescue nil

        # Multiple
        if data != nil
          data.to_hashugar
        else
          raise NotValid, "Couldn't parse the JSON"
        end
      end

      # Module (added recently)
      def module(params={})
        domain = before()

        # Response
        response = Base.get(domain + '/api/v2/ajax/modules?' + URI.encode_www_form(params))
        raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200

        # Data
        data = Oj.load(response.body) rescue nil

        # Multiple
        if data != nil
          data.to_hashugar
        else
          raise NotValid, "Couldn't parse the JSON"
        end
      end

      # Errors
      class NotValid         < StandardError; end
      class MissingArgument  < StandardError; end
      class HTTPError        < StandardError; end
    end
  end
end
